package com.yangzc.lion.common.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.ui.Model;
import org.springframework.web.context.support.WebApplicationObjectSupport;

import com.yangzc.lion.common.dao.HQLBuilderImp;
import com.yangzc.lion.common.dao.HQLInfo;
import com.yangzc.lion.common.model.IBaseModel;
import com.yangzc.lion.common.service.IBaseService;
import com.yangzc.lion.common.tag.Page;

public abstract class BaseController<T extends IBaseModel> extends
		WebApplicationObjectSupport {
	private static final Logger LOGGER = Logger.getLogger(HQLBuilderImp.class);

	protected abstract IBaseService<T, String> getServiceImp();

	public Page<T> findPage(HttpServletRequest request) {
		try {
			String s_pageno = request.getParameter("pageno");
			String s_rowsize = request.getParameter("rowsize");
			String orderby = request.getParameter("orderby");
			String ordertype = request.getParameter("ordertype");
			boolean isReserve = false;
			if (ordertype != null && ordertype.equalsIgnoreCase("desc")) {
				isReserve = true;
			}
			int pageno = 0;
			int rowsize = Page.DEFAULT_PAGESIZE;
			if (s_pageno != null && s_pageno.length() > 0) {
				pageno = Integer.parseInt(s_pageno);
			}
			if (s_rowsize != null && s_rowsize.length() > 0) {
				rowsize = Integer.parseInt(s_rowsize);
			}
			if (isReserve)
				orderby += " desc";
			HQLInfo hqlInfo = new HQLInfo();
			hqlInfo.setOrderBy(orderby);
			hqlInfo.setPageNo(pageno);
			hqlInfo.setPageSize(rowsize);
			changeFindPageHQLInfo(request, hqlInfo);
			Page<T> page = getServiceImp().findPage(hqlInfo);
			return page;
		} catch (Exception e) {
			LOGGER.error("findPage 方法错误", e);
		}
		return Page.getEmptyPage();
	}

	public Page<Map<String, Object>> findObjectPage(HttpServletRequest request) {
		try {
			String s_pageno = request.getParameter("pageno");
			String s_rowsize = request.getParameter("rowsize");
			String orderby = request.getParameter("orderby");
			String ordertype = request.getParameter("ordertype");
			boolean isReserve = false;
			if (ordertype != null && ordertype.equalsIgnoreCase("desc")) {
				isReserve = true;
			}
			int pageno = 0;
			int rowsize = Page.DEFAULT_PAGESIZE;
			if (s_pageno != null && s_pageno.length() > 0) {
				pageno = Integer.parseInt(s_pageno);
			}
			if (s_rowsize != null && s_rowsize.length() > 0) {
				rowsize = Integer.parseInt(s_rowsize);
			}
			if (isReserve)
				orderby += " desc";
			HQLInfo hqlInfo = new HQLInfo();
			hqlInfo.setOrderBy(orderby);
			hqlInfo.setPageNo(pageno);
			hqlInfo.setPageSize(rowsize);
			changeFindPageHQLInfo(request, hqlInfo);
			Page<Map<String, Object>> page = getServiceImp().findObjectPage(
					hqlInfo);
			return page;
		} catch (Exception e) {
			LOGGER.error("findPage 方法错误", e);
		}
		return Page.getEmptyPage();
	}

	protected void changeFindPageHQLInfo(HttpServletRequest request,
			HQLInfo hqlInfo) {

	}
	
	protected void createModel(HttpServletRequest request,
			Model model) {

	}
	
	protected void loadModel(HttpServletRequest request,
			Model model) {

	}
	

}
